import 'package:flutter/material.dart';
import 'package:flutter_app/home/home.dart';
import 'package:flutter_app/message/message.dart';
import 'package:flutter_app/mine/mine.dart';
import 'package:flutter_app/model/user.dart';
import 'package:flutter_app/model/user_redux.dart';
import 'package:flutter_app/shop/shopcar.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final store = Store<UserState>(reducer, initialState: UserState.initState());

  @override
  Widget build(BuildContext context) {
    return StoreProvider<UserState>(
      store: store,
      child: MaterialApp(
        home: MyHomePage(),
        theme: ThemeData(
          primaryColor: Colors.white,
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController controller;

  final List<String> titles = ["首页", "购物车", "会话", "我的"];
  String title;

  @override
  void initState() {
    super.initState();
    title = titles[0];
    controller = new TabController(initialIndex: 0, length: 4, vsync: this);
    controller.addListener(() {
      changeTitle(titles[controller.index]);
    });
  }

  changeTitle(String title) {
    setState(() {
      this.title = title;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text(title),
        centerTitle: true,
      ),
      drawer: Drawer(
          child: new Column(
        children: <Widget>[
          Container(
            child: Column(
              children: <Widget>[
                Container(
                  child: CircleAvatar(
                    radius: 36.0,
                    backgroundImage: AssetImage("images/icon_header.jpg"),
                  ),
                  alignment: Alignment.centerLeft,
                ),
                StoreConnector<UserState, User>(
                  converter: (store) => store.state.user,
                  builder: (context, model) {
                    return Container(
                      child: Text(
                          model?.accountName ?? "未登录",
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      ),
                      alignment: Alignment.topLeft,
                      margin: EdgeInsets.only(top: 10),
                    );
                  },
                ),
                Container(
                  child: Text(
                    "1484288157@qq.com",
                    style: TextStyle(
                      color: Colors.white,
                    ),
                  ),
                  alignment: Alignment.topLeft,
                  margin: EdgeInsets.only(top: 10),
                ),
              ],
            ),
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: NetworkImage(
                      "http://t2.hddhhn.com/uploads/tu/201612/98/st93.png"),
                  fit: BoxFit.fill),
            ),
            padding: EdgeInsets.fromLTRB(15, 25, 15, 10),
          ),
          ListTile(
            leading: new Icon(Icons.refresh),
            title: new Text("刷新"),
          ),
          ListTile(
            leading: new Icon(Icons.help),
            title: new Text("帮助"),
          ),
          ListTile(
            leading: new Icon(Icons.chat),
            title: new Text("会话"),
          ),
          ListTile(
            leading: new Icon(Icons.settings),
            title: new Text("设置"),
          ),
        ],
      )),
      backgroundColor: Colors.grey,
      body: TabBarView(
        controller: controller,
        physics: NeverScrollableScrollPhysics(), //禁止滑动
        children: <Widget>[
          MainPage(),
          ShopCarPage(),
          MessagePage(),
          MinePage(),
        ],
      ),
      bottomNavigationBar: Material(
          color: Colors.white,
          child: TabBar(
              labelPadding: EdgeInsetsDirectional.fromSTEB(0, 5, 0, 0),
              controller: controller,
              labelColor: Colors.blue,
              indicatorColor: Colors.transparent,
              //labelStyle: TextStyle(fontWeight: FontWeight.w600),
              unselectedLabelColor: Colors.black26,
              tabs: [
                Tab(
                  child: Column(
                    children: <Widget>[Icon(Icons.home), Text("主页")],
                  ),
                ),
                Tab(
                  child: Column(
                    children: <Widget>[Icon(Icons.shopping_cart), Text("购物车")],
                  ),
                ),
                Tab(
                  child: Column(
                    children: <Widget>[Icon(Icons.message), Text("会话")],
                  ),
                ),
                Tab(
                  child: Column(
                    children: <Widget>[Icon(Icons.person), Text("我的")],
                  ),
                ),
              ])),
    ));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
